package com.example.mr5.money.view;/**
 * Created by Mr.5
 * Date：2016/9/30.
 * Time：23:46
 */

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

/**
 * Created by Mr.5
 * Date：2016/9/30.
 * Time：23:46
 */
public class ChartView extends View {
    public int XPoint = 60;    //原点的X坐标
    public int YPoint;     //原点的Y坐标
    public int XScale = 55;     //X的刻度长度
    public int YScale = 40;     //Y的刻度长度
    public int XLength;        //X轴的长度
    public int YLength;        //Y轴的长度
    public String[] XLabel;    //X的刻度
    public String[] YLabel;    //Y的刻度
    public String[] Data;      //数据
    public String Title;    //显示的标题
    private WindowManager wm;
    private Paint paint;
    private Paint paint1;

    public ChartView(Context context) {
        super(context);

    }
    public ChartView(Context context, AttributeSet attrs){
        super(context,attrs);
    }

    public void SetInfo(String[] XLabels, String[] YLabels, String[] AllData, String strTitle) {
        XLabel = XLabels;
        YLabel = YLabels;
        Data = AllData;
        Title = strTitle;

        wm = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
        XLength = wm.getDefaultDisplay().getWidth();
        YLength = wm.getDefaultDisplay().getHeight()/7;
        YPoint = YLength+10;
        XScale = XLength/5-10;
        YScale = YLength/4-10;
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);//去锯齿
        paint.setColor(Color.BLUE);//颜色

        paint1 = new Paint();
        paint1.setStyle(Paint.Style.STROKE);
        paint1.setAntiAlias(true);//去锯齿
        paint1.setColor(Color.RED);
        paint1.setStrokeWidth(5);
        paint.setTextSize(24);  //设置轴文字大小
        XPoint = (int) paint.measureText("1000")+20;
        XLength -= 2*XPoint;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);//重写onDraw方法

        //canvas.drawColor(Color.WHITE);//设置背景颜色

        paint.setStrokeWidth(5);
        paint.setTextSize(24);  //设置轴文字大小

        //设置Y轴
        canvas.drawLine(XPoint, YPoint - YLength, XPoint, YPoint, paint);   //轴线
        for (int i = 0; i * YScale < YLength; i++) {
            canvas.drawLine(XPoint, YPoint - i * YScale, XPoint + 5, YPoint - i * YScale, paint);  //刻度
            try {
                paint.setStrokeWidth(2);
                canvas.drawText(YLabel[i], 10, YPoint - i * YScale + 5, paint);  //文字
            } catch (Exception e) {
            }
        }
        paint.setStrokeWidth(5);
        canvas.drawLine(XPoint, YPoint - YLength, XPoint - 5, YPoint - YLength + 10, paint);  //箭头
        canvas.drawLine(XPoint, YPoint - YLength, XPoint + 5, YPoint - YLength + 10, paint);
        //设置X轴
        canvas.drawLine(XPoint, YPoint, XPoint + XLength, YPoint, paint);   //轴线
        for (int i = 0; i * XScale < XLength; i++) {
            canvas.drawLine(XPoint + i * XScale, YPoint, XPoint + i * XScale, YPoint - 5, paint);  //刻度
            try {
                paint.setStrokeWidth(2);
                canvas.drawText(XLabel[i], XPoint + i * XScale - 10, YPoint + 30, paint);  //文字
                paint.setStrokeWidth(8);
                //数据值
                if (i > 0 && YCoord(Data[i - 1]) != -999 && YCoord(Data[i]) != -999)  //保证有效数据
                    canvas.drawLine(XPoint + (i - 1) * XScale, YCoord(Data[i - 1]), XPoint + i * XScale, YCoord(Data[i]), paint1);
                canvas.drawCircle(XPoint + i * XScale, YCoord(Data[i]), 2, paint);
                paint.setStrokeWidth(2);
                canvas.drawText(Data[i],XPoint + i * XScale,YCoord(Data[i])-10,paint);
            } catch (Exception e) {
            }
        }
        paint.setStrokeWidth(5);
        canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 10, YPoint - 5, paint);    //箭头
        canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 10, YPoint + 5, paint);
        paint.setTextSize(24);
//        paint.setStrokeWidth(2);
//        canvas.drawText(Title, 150, 50, paint);
    }

    private float YCoord(String y0)  //计算绘制时的Y坐标，无数据时返回-999
    {
        float y;
        try {
            y = Float.parseFloat(y0);
        } catch (Exception e) {
            Log.d("555log", e.getMessage());
            return -999;    //出错则返回-999
        }
        try {
            return YPoint - y * YScale / Integer.parseInt(YLabel[1]);
        } catch (Exception e) {
        }
        return y;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
//        int width  = measureDimension(DEFAULT_WIDTH, widthMeasureSpec);
//        int height = measureDimension(DEFAULT_HEIGHT, heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }
}
